package com.mango.leet.code.week2.game376;

import java.util.Arrays;

/**
 * 100161. 划分数组并满足最大差限制
 * https://leetcode.cn/contest/weekly-contest-376/problems/divide-array-into-arrays-with-max-difference/
 * @author mango
 * @since 2023/12/17
 */
public class LC100161 {
    public static int[][] divideArray(int[] nums, int k) {
        int[][] result = new int[nums.length/3][3];
        Arrays.sort(nums);
        int row = 0;
        for (int i = 0; i < nums.length; i++) {
            result[row][i % 3] = nums[i];
            if ((i+1) % 3 == 0) {
                if (nums[i] - nums[i-2] > k) {
                    return new int[0][0];
                }
                row++;
            }
        }
        return result;
    }
    public static void main(String[] args) {
//        int[] nums = {1,3,4,8,7,9,3,5,1};
//        int k = 2;
//        for (int[] arr : divideArray(nums, k)) {
//            System.out.println(Arrays.toString(arr));
//        }
//        int[] nums2 = {1,3,3,2,7,3};
//        int k2 = 2;
//        for (int[] arr : divideArray(nums2, k2)) {
//            System.out.println(Arrays.toString(arr));
//        }

        int[] nums3 = {6,10,5,12,7,11,6,6,12,12,11,7};
        int k3 = 2;
        for (int[] arr : divideArray(nums3, k3)) {
            System.out.println(Arrays.toString(arr));
        }



    }
}
